package com.zmm.myyimixian.db;

import android.content.Context;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2016/10/22 0022.
 * 网络数据库工具类
 */

public class DataDao {
    private static Context mContext;
    private static DataDao dataDao;

    private DataDao(Context context) {
        mContext = context;
    }

    //得到工具本身的对象
    public static DataDao getDataDao(Context context) {

        if (dataDao == null) {
            synchronized (DataDao.class) {
                dataDao = new DataDao(context);
            }
        }
        return dataDao;
    }

    /*
     * 加入购物车
     * imgUrl ,title, desc ,price,count
     * 图片地址 标题  类型   价格  数量
     */
    public static void addShoppingCart(String imgUrl, String title, String desc, String price, String count, String newPromoType, SaveListener listener) {
        MyUser user = BmobUser.getCurrentUser(mContext, MyUser.class);
        // 添加到数据库
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setImgUrl(imgUrl);
        shoppingCart.setTitle(title);
        shoppingCart.setDesc(desc);
        shoppingCart.setPrice(price);
        shoppingCart.setCount(count);
        shoppingCart.setNewPromoType(newPromoType);
        //添加一对一关联
        shoppingCart.setMyUser(user);
        shoppingCart.save(mContext, listener);
    }

    /**
     * 取出当前用户购物车里所有的数据
     */
    public static void getAllShoppingCarts(FindListener<ShoppingCart> findListener) {
        MyUser user = BmobUser.getCurrentUser(mContext, MyUser.class);
        BmobQuery<ShoppingCart> query = new BmobQuery<ShoppingCart>();
        query.addWhereEqualTo("myUser", user);    // 查询当前用户的购物车所有数据
        query.findObjects(mContext, findListener);
    }

    /**
     * 获取当前用户购物车里的某个数据
     */
    public static void getShoppingCart(String title, FindListener<ShoppingCart> findListener) {
        MyUser user = BmobUser.getCurrentUser(mContext, MyUser.class);
        BmobQuery<ShoppingCart> query = new BmobQuery<ShoppingCart>();
        query.addWhereEqualTo("myUser", user);    // 查询当前用户的购物车所有数据
        query.addWhereEqualTo("title", title);    // 查询标题数据
        query.findObjects(mContext, findListener);
    }

    /**
     * 修改当前用户购物车里指定的数据
     */
    public static void setShoppingCart(String title, final String count) {
        getShoppingCart(title, new FindListener<ShoppingCart>() {
            @Override
            public void onSuccess(List<ShoppingCart> list) {
                if (list.size() > 0) {//大于0说明有数据
                    ShoppingCart shoppingCart = new ShoppingCart();
                    shoppingCart.setCount(count);
                    shoppingCart.update(mContext, list.get(0).getObjectId(), new UpdateListener() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onFailure(int i, String s) {

                        }
                    });
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    /**
     * 删除当前用户购物车里指定的数据
     */
    public static void deleteShoppingCart(String title, final DeleteListener listener) {
        getShoppingCart(title, new FindListener<ShoppingCart>() {
            @Override
            public void onSuccess(List<ShoppingCart> list) {
                if (list.size() > 0) {//大于0说明有数据
                    ShoppingCart shoppingCart = new ShoppingCart();
                    shoppingCart.setObjectId(list.get(0).getObjectId());
                    shoppingCart.delete(mContext, listener);
                }
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    /**
     * 修改当前用户购物车里指定的数据
     * 有回调监听的方法
     * 修复没有网络的时候 进度条不消失的问题
     */
    public static void setShoppingCart(String title, final String count, final ProgressBar shoppingProgress, final UpdateListener listener) {
        getShoppingCart(title, new FindListener<ShoppingCart>() {
            @Override
            public void onSuccess(List<ShoppingCart> list) {
                if (list.size() > 0) {//大于0说明有数据
                    ShoppingCart shoppingCart = new ShoppingCart();
                    shoppingCart.setCount(count);
                    shoppingCart.update(mContext, list.get(0).getObjectId(), listener);
                }
            }

            @Override
            public void onError(int i, String s) {
                shoppingProgress.setVisibility(View.GONE);
                Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 删除当前用户购物车里指定的数据
     * 有回调监听的方法
     * 修复没有网络的时候 进度条不消失的问题
     */
    public static void deleteShoppingCart(String title, final ProgressBar shoppingProgress, final DeleteListener listener) {
        getShoppingCart(title, new FindListener<ShoppingCart>() {
            @Override
            public void onSuccess(List<ShoppingCart> list) {
                if (list.size() > 0) {//大于0说明有数据
                    ShoppingCart shoppingCart = new ShoppingCart();
                    shoppingCart.setObjectId(list.get(0).getObjectId());
                    shoppingCart.delete(mContext, listener);
                }
            }

            @Override
            public void onError(int i, String s) {
                shoppingProgress.setVisibility(View.GONE);
                Toast.makeText(mContext, "网络异常", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 将收获地址加入数据库
     *
     * @param address
     * @param listener
     */
    public static void addAddress(Address address, SaveListener listener) {
        MyUser user = BmobUser.getCurrentUser(mContext, MyUser.class);
        //添加一对一关联
        address.setMyUser(user);
        address.save(mContext, listener);
    }

    /**
     * 取出当前用户所有的收货地址
     */
    public static void getAllAddress(FindListener<Address> findListener) {
        MyUser user = BmobUser.getCurrentUser(mContext, MyUser.class);
        BmobQuery<Address> query = new BmobQuery<Address>();
        query.addWhereEqualTo("myUser", user);    // 查询当前用户的购物车所有数据
        query.findObjects(mContext, findListener);
    }

    /**
     * 删除当前用户收货地址表里指定的数据
     * 有回调监听的方法
     */
    public static void deleteAddress(Address address, DeleteListener listener) {
        address.delete(mContext, listener);
    }

    /**
     * 修改当前用户购物车里指定的数据
     * 有回调监听的方法
     */
    public static void setAddress(Address address,  UpdateListener listener) {
        address.update(mContext, address.getObjectId(), listener);
    }
}
